#!/usr/bin/python
'''
Finds contiguous matches.
'''
import sys

# parameters.
ref_file = sys.argv[1]
axt_file = sys.argv[2]
min = float(sys.argv[3])


def read_file(filepath):
	# define data structure.
	data = {}
	
	# Open file.
	seq = ""
	seqheader = ""
	fin = open(filepath,"r")
	first = True
	for line in fin:
		# Skip comments.	
		if line[0] == "#": continue

		# first time.
		if first == True and line[0] == ">":
			seq = ""
			seqheader = line.strip().replace(">","")
			first = False
			continue		

		# finished seq.
		if line[0] == ">":
			# save data.
			data[seqheader] = seq.strip()

			# Reset variables.
			seq = ""
			seqheader = line.strip().replace(">","")
			continue

		# get seq.
		seq += line.strip()
			
	# Handle last close and return.
	data[seqheader] = seq.strip()
	fin.close()
	return data

# Read in reference file.
ref = read_file(ref_file)


# Read in matches.
fin = open(axt_file)
for line in fin:
	tmp = line.strip().split()
	if tmp == []: continue
	if line.count("Scaffold") == 0: continue
	
	# Tokenize.
	query = tmp[1].replace(">","")
	target = tmp[5].replace(">","")
	qstart = int(tmp[2])
	qstop = int(tmp[3])
	
	# Check for min identity.
	diff = float(qstop - qstart) / len(ref[query])
	if diff > min:
		print line.strip()
